﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessObjects;
using DataObjects.Interface;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer
{
    public class SqlServerRestaurantMenuNormDetailDao : IRestaurantMenuNormDetailDao
    {
        public IList<RestaurantMenuNormDetail> GetRestaurantMenuNormDetailsByCategory(string menuNormId)
        {
            string procedures = "Get_All_RestaurantMenuNormDetails_ByCondition";

            if (menuNormId == "")
            {
                object[] parms = { "@WhereClause", " WHERE MenuNormID LIKE NULL" };
                return Db.ReadList(procedures, true, Make, parms);
            }
            else
            {
                object[] parms = { "@WhereClause", " WHERE MenuNormID LIKE '%" + menuNormId + "%'" };
                return Db.ReadList(procedures, true, Make, parms);
            }
        }

        public IList<RestaurantMenuNormDetail> GetRestaurantMenuNormDetailsByBranch(string restaurantId)
        {
            string procedures = "Get_All_RestaurantMenuNormDetails_ByCondition";

            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId + "'" };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public void InsertRestaurantMenuNormDetail(RestaurantMenuNormDetail restaurantMenuNormDetail)
        {
            string sql = "Insert_RestaurantMenuNormDetail";

            restaurantMenuNormDetail.MenuNormDetailID = Db.Insert(sql, true, Take(restaurantMenuNormDetail));
        }

        public void DeleteRestaurantMenuNormDetailByCategory(string menuNormId)
        {
            string sql = "Delete_RestaurantMenuNormDetail_ByCategory";

            object[] parms = { "@MenuNormID", menuNormId };
            Db.Update(sql, true, parms);
        }

        private static Func<IDataReader, RestaurantMenuNormDetail> Make = reader =>
            new RestaurantMenuNormDetail
            {
                MenuNormDetailID = reader["MenuNormDetailID"].AsString(),
                MenuNormID = reader["MenuNormID"].AsString(),
                InventoryItemID = reader["InventoryItemID"].AsString(),
                InventoryItemName = reader["InventoryItemName"].AsString(),
                UnitID = reader["UnitID"].AsString(),
                UnitPrice = decimal.Parse(reader["UnitPrice"].AsString()),
                RestaurantID = reader["RestaurantID"].AsString(),
                Quantity = float.Parse(reader["Quantity"].AsString())
            };

        private object[] Take(RestaurantMenuNormDetail restaurantMenuNormDetail)
        {
            return new object[]  
            {
                "@MenuNormDetailID", restaurantMenuNormDetail.MenuNormDetailID,
                "@MenuNormID", restaurantMenuNormDetail.MenuNormID,
                "@InventoryItemID", restaurantMenuNormDetail.InventoryItemID,
                "@InventoryItemName", restaurantMenuNormDetail.InventoryItemName,
			    "@UnitID", restaurantMenuNormDetail.UnitID,
                "@UnitPrice", restaurantMenuNormDetail.UnitPrice ,
                "@RestaurantID", restaurantMenuNormDetail.RestaurantID ,
                "@Quantity", restaurantMenuNormDetail.Quantity 
            };
        }
    }
}